home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / texttors / nyedit21 / macros.arc / UNDO < prev    next >
Encoding:
Text File  |  1988-08-05  |  5.1 KB  |  194 lines

  1. /*========================================================================*/
  2. /* UNDO - implements a simple undo facility for the ME Text Editor.       */
  3. /*    The undo is fairly simple, but will work for character inserts      */
  4. /*    and deletes. Feel free to add more sophisticated processing.        */
  5. /*                                                                        */
  6. /*   To load :         ME -lundo <file>                                   */
  7. /*   To undo a cmd:    Press <GREY MINUS>                                 */
  8. /*                                                                        */
  9. /*  Kludger - Marc Adler / Magma Systems   3/88                           */
  10. /*                                                                        */
  11. /*========================================================================*/
  12.  
  13. #include mekeys.h
  14.  
  15. #define MAXUNDO   100           /* The size of our UNDO queue */
  16. #define NO_CMD    -1
  17.  
  18. int UndoCmd[MAXUNDO];           /* The command which the user pressed */
  19. int UndoCol[MAXUNDO];           /* The cursor column at the time      */
  20. int UndoLnum[MAXUNDO];          /* The line number at the time        */
  21. string UndoText[MAXUNDO];       /* The text that was changed          */
  22. int UndoIndex;                  /* The current position in the queue  */
  23.  
  24.  
  25. /* Init() - called once when this macro is loaded into ME */
  26. init()
  27. {
  28.   UndoIndex = 0;
  29.   assign_key("new_undo", SH_MINUS);
  30.  
  31.   /* Clear out the undo queue */
  32.   for (i = 1;  i <= MAXUNDO;  i++)
  33.     UndoCmd[i] = NO_CMD;
  34.  
  35.   /* Event number 8 is trigger just before a command is dispatched */
  36.   add_hook(8, "queue_cmd");
  37. }
  38.  
  39.  
  40. /* Queue_Cmd - called just before a keystroke is dispatched */
  41. queue_cmd()
  42. {
  43.   /* Increment the queue pointer, and wrap around if at the end */
  44.   UndoIndex++;
  45.   if (UndoIndex > MAXUNDO)
  46.     UndoIndex = 1;
  47.     
  48.   /* Fill the current entry with the key pressed, column, line num */
  49.   UndoCmd[UndoIndex]  = cmd = get_lastkey();
  50.   UndoCol[UndoIndex]  = currcol();
  51.   UndoLnum[UndoIndex] = currlinenum();
  52.   UndoText[UndoIndex] = "";
  53.   
  54.   /* If a self-insert key was pressed, record the char under the cursor */
  55.   if (cmd >= ' ' && cmd < 127)
  56.   {
  57.     UndoText[UndoIndex] = chr(currchar());
  58.     /* A trick! A negative typeable char means that the char was overstruck */
  59.     if (!get_option("in"))
  60.       UndoCmd[UndoIndex] = -cmd;
  61.     return;
  62.   }
  63.  
  64.   /* Special processing */
  65.   switch (cmd)
  66.   {
  67.     case _DELCHAR :
  68.       UndoText[UndoIndex] = chr(currchar());
  69.       break;
  70.       
  71.     case BACKSPACE :
  72.       UndoText[UndoIndex] = substr(currline(), currcol() - 1, 1);
  73.       break;
  74.  
  75.     case SH_FIVE :      /* DelEOL */
  76.       UndoText[UndoIndex] = substr(currline(), currcol(), 256);
  77.       break;
  78.  
  79.     case SH_DEL :       /* DelWord */
  80.       save_position();
  81.       restore_position();
  82.       break;
  83.  
  84.     case F1 :           /* DelLine */
  85.       UndoText[UndoIndex] = currline();
  86.       break;
  87.  
  88.     default :
  89.       break;
  90.   }
  91. }
  92.  
  93.  
  94. new_undo()
  95. {
  96.   if (UndoIndex == 0 || UndoCmd[UndoIndex] == -1)
  97.   {
  98.     message("No commands to undo");
  99.     return;
  100.   }
  101.  
  102.   cmd = UndoCmd[UndoIndex];
  103.   
  104.   if ((cmd >= ' ' && cmd < 127) || cmd < 0)
  105.   {
  106.     backspace();
  107.     if (cmd < 0)        /* overstruck text */
  108.     {
  109.       insert(UndoText[UndoIndex]);
  110.       left();
  111.     }
  112.     goto bye;
  113.   }
  114.  
  115.   if (is_cursor_motion(cmd))
  116.   {
  117.     restore_cursor();
  118.     goto bye;
  119.   }
  120.  
  121.   switch (cmd)
  122.   {
  123.     case _DELCHAR :
  124.       insert(UndoText[UndoIndex]);
  125.       left();
  126.       break;
  127.  
  128.     case BACKSPACE :
  129.       insert(UndoText[UndoIndex]);
  130.       break;
  131.  
  132.     case SH_FIVE : /* DelEOL */
  133.       insert(UndoText[UndoIndex]);
  134.       restore_cursor();
  135.       break;
  136.  
  137.     case SH_DEL  : /* DelWord */
  138.       insert(UndoText[UndoIndex]);
  139.       restore_cursor();
  140.       break;
  141.  
  142.     case F1 :   /* DelLine */
  143.       insert(UndoText[UndoIndex]);
  144.       restore_cursor();
  145.       break;
  146.  
  147.     case F2 :   /* InsLine */
  148.       restore_cursor();
  149.       delline();
  150.       break;
  151.  
  152.     case SH_F2 : /* AppndLine */
  153.       restore_cursor();
  154.       delline();
  155.       break;
  156.  
  157.     default    :
  158.       break;
  159.   }
  160.  
  161. bye:
  162.   message("Command Undone");
  163.  
  164.   /* Dequeue the command */
  165.   UndoCmd[UndoIndex] = -1;
  166.   UndoIndex--;
  167.   if (UndoIndex <= 0)
  168.     UndoIndex = MAXUNDO;
  169. }
  170.  
  171. /*-------------------------------------------------------------------------*/
  172.  
  173. /* is_cursor_motion - returns TRUE if the cmd was a command that simply */
  174. /*                    moved the cursor position.                        */
  175. is_cursor_motion(cmd)
  176.   int  cmd;
  177. {
  178.   if (cmd == _UP  || cmd == _DOWN || cmd == _LEFT     || cmd == _RIGHT    ||
  179.       cmd == HOME || cmd == END   || cmd == CTRL_HOME || cmd == CTRL_END  ||
  180.       cmd == PGUP || cmd == PGDN  || cmd == CTRL_PGUP || cmd == CTRL_PGDN ||
  181.       cmd == SH_PGDN || cmd == SH_PGUP ||
  182.       cmd == CTRL_LEFT || cmd == CTRL_RIGHT ||
  183.       cmd == F5 || cmd == SH_F5 || cmd == ALT_G || cmd == CTRL_B)
  184.     return 1;
  185.     
  186.   return 0;
  187. }
  188.  
  189. restore_cursor()
  190. {
  191.   goline(UndoLnum[UndoIndex]);
  192.   setcol(UndoCol[UndoIndex]);
  193. }
  194.